/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tiebreak.UI;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JMenuBar;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

/**
 *
 * @author AngelwingedDevil
 */
public class RootWindow extends JFrame
{

    private JScrollPane menuPane;
    private JList menu;
    private TieBreakContentPanel contentPane;

    public RootWindow()
    {
        try
        {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            SwingUtilities.updateComponentTreeUI(this);
            this.pack();
        }
        catch (Exception e)
        {
            Logger.getLogger(RootWindow.class.getName()).log(Level.SEVERE, null, e);
        }
        MenuBarCreator mb = new MenuBarCreator();
        setJMenuBar(mb.createMenuBar());
        setTitle("Tie break");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        initComponents();
        setMinimumSize(new Dimension(500,300));
        setSize(getMinimumSize());
        setVisible(true);
    }

    private void initComponents()
    {
        MouseListener ml = new MouseListener()
        {

            public void mouseClicked(MouseEvent e)
            {
                if (e.getClickCount() > 1)
                {
                    try {
                        setContent();
                    } catch (SQLException ex) {
                        Logger.getLogger(RootWindow.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }

            public void mousePressed(MouseEvent e)
            {
            }

            public void mouseReleased(MouseEvent e)
            {
            }

            public void mouseEntered(MouseEvent e)
            {
            }

            public void mouseExited(MouseEvent e)
            {
            }
        };

        getContentPane().setLayout(new GridBagLayout());
        menu = new JList();
        menu.addMouseListener(ml);
        menuPane = new JScrollPane(menu);
        //menuPane.add(menu);
        menuPane.setViewportView(menu);
        menuPane.setVerticalScrollBarPolicy(menuPane.VERTICAL_SCROLLBAR_ALWAYS);
        contentPane = new TieBreakContentPanel();

        
        
        setupMenu();
        getContentPane().setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
        GridBagConstraints g = new GridBagConstraints();
        g.gridx = GridBagConstraints.RELATIVE;
        g.gridy = 0;
        g.weightx = 0;
        g.weighty = 1;
        g.anchor = GridBagConstraints.SOUTH;
        g.insets = new Insets(TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.SMALLSPACE);
        g.fill = GridBagConstraints.VERTICAL;
        getContentPane().add(menuPane, g);
        g.weightx = 1;
        g.fill = GridBagConstraints.BOTH;
        g.insets = new Insets(TieBreakConstraints.MEDIUMSPACE, 0, TieBreakConstraints.MEDIUMSPACE, TieBreakConstraints.MEDIUMSPACE);
        getContentPane().add(contentPane, g);

    }

    private void setupMenu()
    {
        DefaultListModel model = new DefaultListModel();
        model.addElement(new MenuItem("Home", MenuController.HOME));
        model.addElement(new MenuItem("Member Management", MenuController.MEMBER_MANAGEMENT));
        model.addElement(new MenuItem("Accounting", MenuController.ACCOUNTING));
        model.addElement(new MenuItem("Training", MenuController.TRAINING));
        menu.setModel(model);
    }

    private void setContent() throws SQLException
    {
        contentPane.ChangeContent(getContent((MenuItem) menu.getSelectedValue()));
        pack();
    }

    private TieBreakContentPane getContent(MenuItem go) throws SQLException
    {
        TieBreakContentPane b = null;
        switch (go.getID())
        {
            case MenuController.HOME:
                break;
            case MenuController.MEMBER_MANAGEMENT:
                b= new MemberTabbedPane();
                break;
            case MenuController.ACCOUNTING:
                b = new AccountingTab();
                break;
            case MenuController.TRAINING:
                b= new TieBreakTrainingTab();
                break;
        }
        return b;
    }
}
